<?php
namespace app\admin\controller;
use app\base\controller\Needpay;
use think\cache\driver\Redis;
use think\Db;
use think\Controller;

class Index extends Controller
{
    /**
     * use  首页报表
     * by   hugh
     * time 2020/7/9 13:26
     */
    public function welcome()
    {

        return view();
    }



    public function index(){
        $user = get_user();
        $userid = $user['id'];
        $roleid = $user['role_id'];
        $sql = "select * from mh_s_menu where status = '正常' and id 
            in ( 
            select menu_id from mh_s_role_menu_button 
                WHERE role_id = $roleid and button_id = 0 union all select menu_id
                    from mh_s_user_menu_button 
                        WHERE user_id = $userid and button_id = 0 ) order by sort";

        $data = Db::query($sql);
        $this->assign("data",$data);
        $this->assign("user",$user);
        $redis = new Redis();
        $token = $redis->get(get_user_id());
        $this->assign('token',json_encode($token));
        return view();
    }

    /**
     * 编辑当前用户信息
     * by xj 2018/5/25
     * @return \think\response\View
     * @throws \think\Exception
     */
    public function edit()
    {
        //访问页面
        if ($this->request->isGet()) {
            //获取当前用户ID
            $id = get_user_id();
            $user = Db::table('s_user')->where(["id"=>$id])->find();
            $this->assign("user",$user);
            return view();
        }

        if ($this->request->isPost()) {

            //获取参数
            $id = get_user_id();

            $password = input("password");
            $name = input("name");
            $sex = input("sex");
            $mobile = input("mobile");
            $mail = input("mail");

            //拼装数据
            $data = array();
            $data['name'] = $name;
            $data['sex'] = $sex;
            $data['mobile'] = $mobile;
            $data['mail'] = $mail;

            if(!empty($password)){
                $data['password'] = $password;
            }

            Db::name("s_user")->where(["id"=>$id])->update($data);

            wlog("当前用户编辑","编辑成功，用户名称:".$name);
            if(!empty($password)){
                session('MAP_USER', null);
                session_destroy();
                return json(array('code' => 0,'text'=>'操作成功!'));
            }else{

                //返回
                return json(array('code' => 0,'text'=>'操作成功!'));
            }

        }
    }
}
